'use strict';

const Service = require('egg').Service;
class MovieService extends Service {
  async find(m) {
    const db = this.app.mongodb;
    const res = await db.collection(m).find().toArray();
    return res;
  }
  async getMovieDetail(id){
    const db = this.app.mongodb;
    const tables = ["top250","inTheaters","comingSoon"];
    const sum = [];
    for(var i=0;i<tables.length;i++){
        var res = await db.collection(tables[i]).find({_id:id}).toArray();
        console.log(res);
        sum.push(...res);
    }
    return  sum;
  }
  async getMovieSearch(keyword){
    const db = this.app.mongodb;
    const tables = ["top250","inTheaters","comingSoon"];
    const sum = [];
    for(var i=0;i<tables.length;i++){
        keyword = new RegExp(keyword);
        /* find({title:"你"}) error 
         find({title:/你/})  success */
        var res = await db.collection(tables[i]).find({title:keyword}).toArray();
        console.log(res);
        sum.push(...res);
    }
    return  sum;
  }
}

module.exports = MovieService;
